<?php
/*
 * class db
*/

class dbAdapter {
	var $_table = NULL;
	var $_conn = NULL;
	var $_lastSQL = NULL;
	var $_lastSucceed = NULL;
	var $_selectSchema = NULL;
	var $_lastAffectedRows = NULL;
	var $_lastId = NULL;
	
	function adp_init($cfg) {
	}
	
	function _dumpWhere($where) {
		// 'id=10 and var in (1,2,3) or ta>10'
		$def = array(
			'and',
			'or',
			'in'
		);
		$exp = "/(?:(?:=|>|>=|<|<=)(\w+))|\(((\w+),?)+\)/";
	}
	/*
	 * function get
	 * @param $key
	*/
	
	function _dumpSQL($type) {
		$args = func_get_args();
		array_shift($args);
		switch ($type) {
			case 'select':
				var_dump($args);
				list($where, $selected, $ordered, $starts, $limits) = $args;
				$orderby = "";
				if (!empty($where)) foreach ($where as $k => $v) {
					$ws[] = $k . "='" . $v . "'";
				}
				$ws = join(" and ", $ws);
				if (is_array($selected)) {
					$ss = join(" , ", $selected);
				}
				else {
					$ss = $selected;
				}
				if (isset($ordered)) {
					$orderby = sprintf(" order by %s", $ordered);
				}
				if (isset($starts)) {
					$limitsby = sprintf(" limit %s%s", $starts, isset($limits) ? ",{$limits}" : "");
				}
				else {
					$limitsby = "";
				}
				return sprintf("select {$ss} from " . $this->_table . " where {$ws}{$orderby}{$limitsby};");
				break;
			case 'insert':
				break;
			case 'delete':
				break;
			case 'update':
				break;
			}
	}
	
	function get($key) {
		return $this->{'_' . $key};
	}
	/*
	 * function __initLast
	 * @param
	*/
	
	function __initLast() {
		$this->_lastAffectedRows = NULL;
		$this->_lastSQL = NULL;
		$this->_lastSucceed = NULL;
	}
	/*
	 * function __new
	 * @param $cfg,$type='mysql'
	*/
	
	function __new($cfg, $type = 'mysql') {
		return new $type($cfg);
	}
	/*
	 * function setTable
	 * @param $table
	*/
	
	function setTable($table) {
		$this->_table = $table;
		return $this;
	}
	/*
	 * function insert
	 * @param $data
	*/
	
	function insert($data) {
		$this->__initLast();
	}
	
	function select($where, $selected) {
	}
	/*
	 * function update
	 * @param $data,$where
	*/
	
	function update($data, $where) {
	}
	/*
	 * function delete
	 * @param $where
	*/
	
	function delete($where) {
	}
	/*
	 * function fetch
	 * @param $columns,$where
	*/
	
	function fetchOne($columns, $where) {
	}
	/*
	 * function fetchAll
	 * @param $columns,$where,$cusor,$count
	*/
	
	function fetchAll($columns, $where, $cusor, $count) {
	}
}
?>